using System.IO;

namespace Engine.FileFormats3D.C11Syndikat.QADFile
{
    public class BlockInfo
    {
        #region Internal Fields

        internal uint numberOfPolygons;

        #endregion Internal Fields

        #region Private Fields

        private float blockY;
        private ushort cellX;
        private ushort cellY;
        private uint chunk65K;
        private uint firstPolygon;
        private uint firstSurfaceChunk;
        private int numberOfSurfaceChunks;
        private ushort value11;
        private int value3;
        private int value4;
        private float x;
        private float y;
        private float z;

        #endregion Private Fields

        #region Public Constructors

        public BlockInfo(
                    ushort cellX,// cellPosition X
                    ushort cellY,// cellPostiion Y
                    uint firstPolygon,//first polgon
                    uint numberOfPolygons,//number of Polygons in Block
                    uint firstTexture,//firstTexture??
                    int numberOfTextures,//number of surfaces??
                    float x,
                    float y,
                    float z,
                    float yHeight,// Y-Height
                    int value3,
                    int value4,
                    ushort chunkID,
                    ushort value11
                        )
        {
            this.cellX = cellX;// cellPosition X
            this.cellY = cellY;// cellPostiion Y
            this.firstPolygon = firstPolygon;//first polgon
            this.numberOfPolygons = numberOfPolygons;//number of Polygons in Block
            this.firstSurfaceChunk = firstTexture;//firstTexture??
            this.numberOfSurfaceChunks = numberOfTextures;//number of textures??
            this.x = x;
            this.y = y;
            this.z = z;
            this.blockY = yHeight;// Y-Height
            this.value3 = value3;
            this.value4 = value4;
            this.chunk65K = chunkID;
            this.value11 = value11;
        }

        public BlockInfo(int cellX, int cellY)
        {
            this.blockY = 0f;
            this.cellX = (ushort)cellX;
            this.cellY = (ushort)cellY;
            this.chunk65K = 0;
            this.firstPolygon = 0;
            this.firstSurfaceChunk = 0;
            this.numberOfPolygons = 0;
            this.numberOfSurfaceChunks = -1;
            this.value11 = 0;
            this.value3 = 65535;
            this.value4 = 65535;
            this.x = 0;
            this.y = 0;
            this.z = 0;
        }

        #endregion Public Constructors

        #region Public Properties

        public float BlockY
        {
            get { return blockY; }
            set { blockY = value; }
        }

        public ushort CellX
        {
            get { return cellX; }
        }

        public ushort CellY
        {
            get { return cellY; }
        }

        public uint Chunk65K
        {
            get { return chunk65K; }
            set { this.chunk65K = value; }
        }

        public uint FirstPolygon
        {
            get { return firstPolygon; }
            set { this.firstPolygon = value; }
        }

        public uint FirstSurfaceChunk
        {
            get { return firstSurfaceChunk; }
            set { this.firstSurfaceChunk = value; }
        }

        public int NumberOfSurfaceChunks
        {
            get { return numberOfSurfaceChunks; }
            set { this.numberOfSurfaceChunks = value; }
        }

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public float Z
        {
            get { return z; }
            set { z = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public static BlockInfo FromStream(BinaryReader br)
        {
            ushort cellPositionX = br.ReadUInt16();// cellPosition X
            ushort cellPositionY = br.ReadUInt16();// cellPostiion Y
            uint firstPolygon = br.ReadUInt32();//first polgon
            uint numberOfPolygonsInBlock = br.ReadUInt32();//number of Polygons in Block
            uint firstSurfaceChunk = br.ReadUInt32();//firstTexture??
            int numberOfSurfaceChunks = br.ReadInt32();//number of textures??
            float x = br.ReadSingle();
            float y = br.ReadSingle();
            float z = br.ReadSingle();
            float height = br.ReadSingle();// Y-Height
            int value3 = br.ReadInt32();
            int value4 = br.ReadInt32();
            ushort chunk65K = br.ReadUInt16();
            ushort value11 = br.ReadUInt16();

            return new BlockInfo(cellPositionX,// cellPosition X
                                 cellPositionY,// cellPostiion Y
                                 firstPolygon,//first polgon
                                 numberOfPolygonsInBlock,//number of Polygons in Block
                                 firstSurfaceChunk,//firstTexture??
                                 numberOfSurfaceChunks,//number of textures??
                                 x,
                                 y,
                                 z,
                                 height,// Y-Height
                                 (int)value3,
                                 (int)value4,
                                 chunk65K,
                                 value11
                                 );
        }

        public override string ToString()
        {
            return cellX.ToString() + "\t" + cellY.ToString() + "\t" + firstPolygon.ToString() + "\t" +
                numberOfPolygons.ToString() + "\t" + firstSurfaceChunk.ToString() + "\t" + numberOfSurfaceChunks.ToString() + "\t" + X.ToString() + "\t" + Y.ToString() + "\t" + Z.ToString() + "\t" +
                blockY.ToString() + "\t" + value3.ToString() + "\t" + value4.ToString() + "\t" + chunk65K.ToString();
        }

        #endregion Public Methods

        #region Internal Methods

        internal void ResetPolygonData()
        {
            this.chunk65K = 0;
            this.firstPolygon = 0;
            this.firstSurfaceChunk = 0;
            this.numberOfPolygons = 0;
            this.numberOfSurfaceChunks = 0;
        }

        internal void Save(BinaryWriter bw)
        {
            bw.Write(this.cellX);// cellPosition X
            bw.Write(this.cellY);// cellPostiion Y
            bw.Write(this.firstPolygon);//first polgon
            bw.Write(this.numberOfPolygons);//number of Polygons in Block
            bw.Write(this.firstSurfaceChunk);//first Surface Chunk
            bw.Write((uint)this.numberOfSurfaceChunks);//number of SurfaceChunks??
            bw.Write(this.X);
            bw.Write(this.Y);
            bw.Write(this.Z);
            bw.Write(this.blockY);// Y-Height
            bw.Write(this.value3);
            bw.Write(this.value4);
            bw.Write((ushort)this.chunk65K);
            bw.Write((ushort)this.value11);
        }

        #endregion Internal Methods
    }
}